import Router from 'koa-router';
import axios from './utils/axios'
import Province from '../dbs/models/province'
import Menu from '../dbs/models/menu'
import City from '../dbs/models/city'


let router = new Router({
    // 前缀
    prefix: '/geo'
})

// 定义签名
const sign = '5be6f1e38999ac1af60cbaad320b60cc';

// 获取位置接口

router.get('/getPosition', async (ctx) => {
    let {status,data: {
        province,
        city
    }} = await axios.get(`http://cp-tools.cn/geo/getPosition?sign=${sign}`)
    if (status === 200) {
        ctx.body = {
            province,
            city
        }
    }
    else {
        ctx.body = {
            province: '',
            city: ''
        }
    }
})

// 获取省份接口

router.get('/province', async (ctx) => {
    // const province = await Province.find()
    // ctx.body = {
    //     province: province.map((item) => {
    //         return {
    //             id: item.id,
    //             name: item.value
    //         }
    //     })
    // }
    let {status, data: {province}} = await axios.get(`http://cp-tools.cn/geo/province?sign=${sign}`)
    if (status === 200) {
        ctx.body = {
            province
        }
    }
    else {
        ctx.body = {
            province: []
        }
    }
})

// 获取菜单接口

router.get('/menu', async (ctx) => {
    // 也可以通过数据库获取
    const result = await Menu.findOne()
    ctx.body = {
      menu: result.menu
    }
    // let {status,data: {menu}} =  await axios.get(`http://cp-tools.cn/geo/menu?sign=${sign}`)
    // if (status === 200) {
    //     ctx.body = {
    //         menu
    //     }
    // }
    // else {
    //     ctx.body = {
    //         menu: []
    //     }
    // }
})

// province 带id

router.get('/province/:id', async (ctx) => {
    let {status, data: {
        city
    }} = await axios.get(`http://cp-tools.cn/geo/province/${ctx.params.id}?sign=${sign}`)
    if (status === 200) {
        ctx.body = {
            city
        }
    } else {
        ctx.body = {
            city: []
        }
    }
})


// 获取城市接口

router.get('/city', async (ctx) => {
    let city = []
    const result = await City.find()
    result.forEach((item) => {
        city = city.concat(item.value)
    })
    ctx.body = {
        code: 0,
        city: city.map((item) => {
            return {
                province: item.province,
                id: item.id,
                name: item.name === '市辖区' || item.name === '省直辖县级行政区划' ? item.province : item.name
            }
        })
    }
    // let {status, data: {city}} = await axios.get(`http://cp-tools.cn/geo/city?sign=${sign}`)
    // if (status === 200) {
    //     ctx.body = {
    //         city
    //     }
    // }
    // else {
    //     ctx.body = {
    //         city: []
    //     }
    // }
})


// 获取热门城市接口

router.get('/hotCity', async (ctx) => {
    
    let list = [
    '北京市',
    '上海市',
    '广州市',
    '深圳市',
    '天津市',
    '西安市',
    '杭州市',
    '南京市',
    '武汉市',
    '成都市'
  ]
  // let result = await City.find()
  // let nList = []
  // result.forEach(item => {
  //   nList = nList.concat(item.value.filter(k => list.includes(k.name) || list.includes(k.province)))
  // })
  // ctx.body = {
  //   hots: nList
  // }

    const result = await City.find()
    let nlist = []
    result.forEach((item) => {
        // nlist = nlist.concat(item.value.filter(k => list.includes(k.name) || list.includes(k.province)))
        nlist = nlist.concat(item.value.filter(function (k) {
            // 可以用includes来选择
            return list.includes(k.name) || list.includes(k.province)
        }))
    })
    ctx.body = {
        hots: nlist
    }

    // let {status, data: {hots}} = await axios.get(`http://cp-tools.cn/geo/hotCity?sign=${sign}`)
    // if (status === 200) {
    //     ctx.body = {
    //         hots
    //     }
    // } else {
    //     ctx.body = {
    //         hotCity: []
    //     }
    // }
})

export default router

